using System;
using System.Windows;
using System.Windows.Threading;

namespace MaxAlarm.Common
{
    public static class Extentions
    {
        public static T SafeGetValue<T>(this DependencyObject depObj, DependencyProperty property)
        {
            return (T)depObj.Dispatcher.SafeInvoke(depObj.GetValue, property);
        }

        public static TResult SafeInvoke<T, TResult>(this Dispatcher dispather, Func<T, TResult> function, T value)
        {
            if (dispather.CheckAccess())
            {
                return function(value);
            }

            return (TResult)dispather.Invoke(function, value);
        }

        public static void SafeSetValue(this DependencyObject depObj, DependencyProperty property, Object value)
        {
            depObj.Dispatcher.SafeInvokeAction(depObj.SetValue, property, value);
        }

        public static void SafeSetValue(this DependencyObject depObj, DependencyPropertyKey propertyKey, Object value)
        {
            depObj.Dispatcher.SafeInvokeAction(depObj.SetValue, propertyKey, value);
        }

        public static void SafeInvokeAction<T1, T2>(this Dispatcher dispather, Action<T1, T2> action, T1 value1, T2 value2)
        {
            if (dispather.CheckAccess())
            {
                action(value1, value2);
            }
            else
            {
                dispather.Invoke(action, value1, value2);
            }
        }


    }

}
